<?php
/* KAP event Manager, xmlclass

file version 2005-05-13 10:40:52

noms d'evenements de forme classe::methode

*/

/*--------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------*/
class wakeventManager extends wakCoreComponent {
/*-------------------------------------------------------------------------------------------------
																													constructeur
-------------------------------------------------------------------------------------------------*/
	function wakeventManager(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		//echo wakaffvar($this->parameters,'sqlmanager_param');
		$this->builder=& wakNeedUniqueObject('xmlclassbuilder');
	}
/*--------------------------------------------------------------------------------------------------
																														proprietes
--------------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'path' => 'events/',
		'suffix'=>'.xmlclass.wak-event.xml',
		// fichiers de methodes additionnelles
		'addfiles'=>array(
			'wak-core-0.5/data/mysqlviews.xmlclasslibrary.xml'
		)
	);
	var $objects=array();
/*--------------------------------------------------------------------------------------------------
																											methodes publiques
--------------------------------------------------------------------------------------------------*/
	function runEvent($name,$params=array()) {
		// analyse du nom d'evenement
		$ereg_name='[a-zA-Z][a-zA-Z0-9_]*';
		$ereg='^('.$ereg_name.')((::)|(->))('.$ereg_name.')$';
		if (!ereg($ereg,$name,$found)) {
			trigger_error("wakeventmanager-xmlclass: incorrect event name $name (must be class::method or class->method)",E_USER_WARNING);
			return false;
		}
		//echo affvar($found);
		$class=&$found[1];
		$call=&$found[2];
		$method=&$found[5];
		// execution
		switch ($call) {
			case '::':
				if (!$this->makeDef($class)) return false;
				//return eval("return $class::$method(\$params);");
				return call_user_func(array($class,$method),$params);
				//return {$class}::{$method}($params);
				break;
			case '->':
				//echo "<p>calling {$class}->{$method} with ".affvar($params,'params')."</p>";
				$obj=& $this->getObject($class);// echo affvar($obj);
				if (!is_object($obj)) return false;
				//echo affvar($obj);
				return $obj->$method($params);
				break;
			default:
				trigger_error("wak eventManager-xmlclass: unsupported event call $call",E_USER_WARNING);
				return false;
		}
		// analyse du nom d'evenement
		/*$ereg_name='[a-zA-Z][a-zA-Z0-9_]*';
		$ereg='^('.$ereg_name.')::('.$ereg_name.')$';
		if (!ereg($ereg,$name,$found)) {
			trigger_error("wakeventmanager-xmlclass: incorrect event name $name (must be class::method)",E_USER_WARNING);
			return false;
		}
		$class=&$found[1];
		$method=&$found[2];
		
		// recup de l'objet
		$obj=$this->makeXmlObject($class);
		return $obj->$method($params);*/
	}
/*--------------------------------------------------------------------------------------------------
																												methodes privees
--------------------------------------------------------------------------------------------------*/
	/* renvoie une instance unique de la classe xml désirée
	*/
	/*function & makeXmlObject($class) {
		if (!isset($this->objects[$class])) {
			$file=$this->parameters['path'].$class.$this->parameters['suffix'];
			$this->objects[$class]=$this->builder->build($file);
		}
		return $this->objects[$class];
	}*/
	/* renvoie une instance unique de la classe xml désiré
	*/
	function & getObject($class) {
		if (!isset($this->objects[$class]) or !$this->objects[$class]) {
			if (!$this->makeDef($class)) return false;
			//echo "<p>instanciating $class</p>";
			$this->objects[$class]=& new $class();
		}
		//echo affvar($this->objects[$class]);
		return $this->objects[$class];
	}
	
	function makeDef($class) {
		/*$file=$this->parameters['path'].$class.$this->parameters['suffix'];
		if (!is_file($file)) {
			trigger_error("wak eventManager-classmethods: file $file not found",E_USER_WARNING);
			return false;
		}
		include($file);
		return true;*/
		$mainfile="events/".$class.".xmlclass.wak-event.xml";
		return $this->builder->buildClassFrom($mainfile,$this->parameters['addfiles']);
	}
}
?>
